package com.example.gokchinesefoodmappcdev.controller;

import com.example.gokchinesefoodmappcdev.entity.CustomerInfo;
import com.example.gokchinesefoodmappcdev.entity.PageBean;
import com.example.gokchinesefoodmappcdev.service.impl.CustomerInfoServiceImpl;
import com.example.gokchinesefoodmappcdev.util.ResultData;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;


@RequestMapping("/rest/appUser")
@RestController
public class AppUserController {
    @Autowired
    private CustomerInfoServiceImpl customerInfoService;

    /**
     * 根据条件查询所有用户
     * @param customerInfo
     * @param pageNum
     * @param pageSize
     * @return
     * @throws ParseException
     */
    @RequestMapping("/list")
    public PageBean<CustomerInfo> findAll(CustomerInfo customerInfo, int pageNum, int pageSize) throws ParseException {
        PageBean<CustomerInfo> pageBean = customerInfoService.findCustomerInfoAll(customerInfo, pageNum, pageSize);
        return pageBean;
    }

    /**
     * 查询用户信息
     * @param customerId 用户id
     * @return
     */
    @RequestMapping("/info")
    public ResultData findCustomerInfoAndLocationAddress(Integer customerId) {
        ResultData resultData = customerInfoService.findCustomerInfoAndLocationAddressByCustomerId(customerId);
        return resultData;
    }

    /**
     *
     * 修改用户状态
     * @param customerInfo
     * @return
     */
    @RequestMapping("/update")
    public ResultData updateCustomerInfoStatusByCustomerId(CustomerInfo customerInfo) {
        ResultData resultData = customerInfoService.updateCustomerInfoStatusByCustomerId(customerInfo);
        return resultData;
    }

    /**
     * 导出用户列表
     * @param customerInfo
     */
    @RequestMapping("/outExecl")
    public void outAllCustomerInfoExecl(CustomerInfo customerInfo, HttpServletResponse response) {
        //创建HSSFWorkbook对象,  excel的文档对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //excel的表单
        HSSFSheet sheet = workbook.createSheet("用户列表");
        List<CustomerInfo> customerInfoList = customerInfoService.outAllCustomerInfoExecl(customerInfo);
        //设置要导出的文件的名字
        String fileName = "customerInfo"+".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = { "用户id", "用户名", "用户帐号", "注册时间","状态"};
        //headers表示excel表中第一行的表头
        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        // 时间格式化
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //在表中存放查询到的数据放入对应的列
        for (CustomerInfo customerInfo1 : customerInfoList) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(customerInfo1.getCustomerId());
            row1.createCell(1).setCellValue(customerInfo1.getNickName());
            row1.createCell(2).setCellValue(customerInfo1.getMobile());
            row1.createCell(3).setCellValue(sdf.format(customerInfo1.getGmtCreate()));
            row1.createCell(4).setCellValue(customerInfo1.getStatus());
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        try {
            response.flushBuffer();
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
